<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/page/common/commonConfig.jsp"%>
<%--
传入本页面可以带有参数约束选择用户信息
max(max=数量) 约束最多选择的用户数量，默认不限制数量
outme(outme=1) 当传入的参数包含outme并且参数的值是1时，就不能选择当前登录用户，默认可以选择当前登录用户
userIds(userIds=id1,id2,id3) 默认已经选择的用户，传入以,分割的字符串，默认无

父页面应该包含以下函数，用来本页面执行确定后调用。
getChooseUsesrResult(ids,names,loginCodes,newaddIds,newaddNames,newaddLoginCodes,deletedIds);
ids是选中的全部id
names是选中的全部name
loginCodes是选中的全部loginCode
newaddIds是在与传入本页参数userIds时配合使用，当存在userIds时候，在基于userIds基础之上新增的全部id
newaddNames配合newaddIds的所有name
newaddLoginCodes配合newaddIds的所有loginCodes
deletedIds是在与传入本页参数userIds时配合使用，当存在userIds时候，删除了userIds中所有id
 --%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>选择用户页面</title>
   	<script src="<%=basePath%>component/tabbar.js"></script>
   	<script src="<%=basePath%>component/layout.js"></script>
   	<script src="<%=basePath%>component/tree.js"></script>
   	<script src="<%=basePath%>component/grid.js"></script>
   	<script src="<%=basePath%>component/toolbar.js"></script>
   	<script type="text/javascript">
var dhxLayout = createLayoutNormal('3L');
var userlistGrid;
var userlistGriddone;
function configLayout(){
	var a = dhxLayout.cells('a');
	var b = dhxLayout.cells('b');
	var c = dhxLayout.cells('c');
	a.setText('检索区');
	b.setText('已选择区');
	c.setText('选择区');
	b.setHeight(150);
	a.setWidth(300);
	a.fixSize(true, true);
	b.fixSize(true, true);
	c.fixSize(true, true);
	b.hideHeader();
	c.hideHeader();
	createTabbarFromDHXLayoutCell(a,operateTabbar);
	createToolbarFromDHXLayoutCell(b,operateToolbarChooseDone);
	createToolbarFromDHXLayoutCell(c,operateToolbarUserList);
	dhxGridUserListChooseDone.initDataTable(dhxLayout,"b");
	dhxGridUserList.initDataTable(dhxLayout,"c");
}

function operateTabbar(tabbar){
	tabbar.addTab("a1", "模糊检索", "80px");
	tabbar.addTab("a2", "单位", "60px");
	tabbar.addTab("a3", "业务角色", "80px");
	tabbar.setContent("a1", "queryUserDiv");
	tabbar.setTabActive("a1");
	initTree(tabbar,"a2");
	dhxGridRoleBusiness.initDataTable(tabbar,"a3");
}

function operateToolbarChooseDone(toolbar){
    toolbar.addText("info", 1, "已选择区");
    toolbar.addSeparator("sep1", 2);
    toolbar.addButton("undo", 3, "取消选择", "undo.gif", "undo_dis.gif");
    toolbar.addButton("selectAll", 4, "全选", "selection.gif", "selection.gif");
    toolbar.addButton("saveAll", 5, "确定", "save.gif", "save_dis.gif");
    if('${empty param.max}' == 'false'){
    	toolbar.addText("infotip", 6, ('最多选择 [ ${param.max} ]个用户'));
	}
    toolbar.attachEvent("onClick", function(id) {
    	if(id == 'undo'){
    		var selids = userlistGriddone.getSelectedId();
    		if(selids != null){
    			var ids = selids.split(',');
    			for(var i = 0;i<ids.length;i++){
					var lastrowIndex = userlistGriddone.getRowIndex(ids[i])-1;
					userlistGriddone.deleteSelectedRows();
					userlistGriddone.selectRow(lastrowIndex);
					for(var i=(lastrowIndex+1) ;i<(userlistGriddone.getRowsNum());i++){
						userlistGriddone.cells(userlistGriddone.getRowId(''+i),0).setValue(i+1);
					}
				}
			}
    	}
    	if(id == 'selectAll'){
    		dhxGridUserListChooseDone.selectAll();
    	}
    	if(id == 'saveAll'){
    		setParentResult();
    	}
    });
}

function operateToolbarUserList(toolbar){
    toolbar.addText("info", 1, "选择区");
    toolbar.addSeparator("sep1", 2);
    toolbar.addButton("redo", 3, "选择添加", "redo.gif", "redo_dis.gif");
    toolbar.addButton("selectAll", 4, "全选", "selection.gif", "selection.gif");
    toolbar.attachEvent("onClick", function(id) {
    	if(id == 'redo'){
	    	var selids = userlistGrid.getSelectedId();
	    	if(selids != null){
	    		var ids = selids.split(',');
	    		for(var i = 0;i<ids.length;i++){
	    			dhxGridUserList.chooseUser(ids[i]);
	    		}
	    	}	
    	}
    	if(id == 'selectAll'){
    		dhxGridUserList.selectAll();
    	}

    });
}

function initTree(tabbar,cell){
	MyTree.initMyTreeFromTabbar(tabbar,cell);
	MyTree.setupTree("<%=basePath%>system-unit/UnitAction!unitTree.action",function(id){
		changeUserList('<%=basePath%>system-user/UserAction!userGridFromUnit.action?userForm.unitId=' + id);
	});
}

var dhxGridRoleBusiness= {
	initDataTable : function (tabbar,cell) {
		var mygrid = createGridFromTabbar(tabbar,cell);
	    mygrid.setHeader("业务角色名称");
	    mygrid.setInitWidths("290");
	    mygrid.setColAlign("center");
	    mygrid.setColTypes("ro");
	    mygrid.attachEvent("onRowSelect", this.openUsers);
	    mygrid.init();
	    setSkin(mygrid);
	    mygrid.loadXML('<%=basePath%>system-roleBusiness/RoleBusinessAction!roleBusinessGridForUser.action');
	},
	openUsers : function(rowID,celInd){
		changeUserList('<%=basePath%>system-user/UserAction!userGridFromRoleBusiness.action?userForm.roleBusinessId=' + rowID);
	}
};

var dhxGridUserList= {
	initDataTable : function (layout,cell) {
		userlistGrid = createGridFromLayout(layout,cell);
	   	userlistGrid.setHeader("序号,登录姓名,登录账号,备注");
		userlistGrid.setInitWidths("40,100,100,*");
		userlistGrid.setColAlign("right,left,left,left");
		userlistGrid.setColSorting("int,str,str,str");
		userlistGrid.setColTypes("ro,ro,ro,ro");
		userlistGrid.selMultiRows = true;
		//userlistGrid.attachEvent("onRowSelect", this.openResource);
		userlistGrid.attachEvent("onRowDblClicked",this.chooseUser);
		userlistGrid.attachEvent("onEnter", this.chooseUser);
		userlistGrid.init();
		setSkin(userlistGrid);
	}
	,chooseUser : function(rowID,celInd){
		var loginName = userlistGrid.cells(rowID, 1).getValue();
		var userName = userlistGrid.cells(rowID, 2).getValue();
		var note = userlistGrid.cells(rowID, 3).getValue();
		if('${param.outme}' == 1 ){
			if(rowID == '${user.userMainId}'){
				myAlert("不能添加自己");
				return ;
			}
		}
		if('${empty param.max}' == 'false'){
			if(userlistGriddone.getRowsNum() >= '${param.max}'){
				return ;
			}
		}
		if(userlistGriddone.getAllRowIds(',').indexOf(rowID)<0){
			userlistGriddone.addRow(rowID, [''+(userlistGriddone.getRowsNum()+1),loginName,userName,note], ''+userlistGriddone.getRowsNum());
		}
		userlistGriddone.selectRowById(rowID);
	},selectAll : function(){
		userlistGrid.selectAll();
	}
};

var dhxGridUserListChooseDone= {
	initDataTable : function (layout,cell) {
		userlistGriddone = createGridFromLayout(layout,cell);
	   	userlistGriddone.setHeader("序号,登录姓名,登录账号,备注");
		userlistGriddone.setInitWidths("40,100,100,*");
		userlistGriddone.setColAlign("right,left,left,left");
		userlistGriddone.setColSorting("int,str,str,str");
		userlistGriddone.setColTypes("ro,ro,ro,ro");
		userlistGriddone.selMultiRows = true;
		userlistGriddone.init();
		setSkin(userlistGriddone);
		if('${empty param.userIds}' == 'false'){
			userlistGriddone.loadXML('<%=basePath%>system-chooseComponent/ChooseUserAction!queryUserInfoByUserIds.action?userIds=${param.userIds}');
		}
	}
	,selectAll : function(){
		userlistGriddone.selectAll();
	}
	,clearAll : function(){
		userlistGriddone.clearAll();
	}
};

function changeUserList(url,func){
	userlistGrid.clearAndLoad(url,func);
}

function queryUser(){
	layoutProgressOn(dhxLayout);
	var loginCode = document.getElementById("loginCode").value;
	document.getElementById("loginCode").style.display = 'none';
	var func = function(){
		layoutProgressOff(dhxLayout);
		document.getElementById("loginCode").style.display = '';
		document.getElementById("loginCode").focus();
	};
	changeUserList('<%=basePath%>system-chooseComponent/ChooseUserAction!queryUserInfoByUser.action?tuser.loginCode='+loginCode,func);
}

$(function (){
	configLayout();
});

function setParentResult(){
	var ids = userlistGriddone.getAllRowIds(',');
	var names = '';
	var loginCodes = '';
	if(ids != ''){
		var idslist = ids.split(',');
		for(var i=0;i<idslist.length;i++){
			names += ( (names==''?'':',')+ userlistGriddone.cells(idslist[i],1).getValue());
			loginCodes += ((loginCodes==''?'':',') + userlistGriddone.cells(idslist[i],2).getValue());
		}
	}
	var newaddIds = ids;
	var deletedIds = '';
	if('${empty param.userIds}' == 'false'){
		var uids = ('${param.userIds}').split(',');
		for(var i = 0;i<uids.length;i++){
			var uid = uids[i];
			if(newaddIds.indexOf(uid) == 0){
				newaddIds = newaddIds.replace((uid+','),'');
				newaddIds = newaddIds.replace((uid),'');
				continue;
			}
			if (newaddIds.indexOf(uid) < 0){
				if(deletedIds == ''){
					deletedIds += uid;
				}else{
					deletedIds +=(','+uid);
				}
				continue;
			}
			if (newaddIds.indexOf(uid) > 0){
				newaddIds = newaddIds.replace((','+uid),'');
				newaddIds = newaddIds.replace((uid),'');
				continue;
			}
		}
	}
	var newaddNames = names;
	var newaddLoginCodes = loginCodes;
	if(newaddIds != ids && newaddIds != ''){
		var newaddNames = '';
		var newaddLoginCodes = '';
		var nids = newaddIds.split(',');
		for(var i=0;i<nids.length;i++){
			var nid = nids[i];
			newaddNames += ( (newaddNames==''?'':',')+ userlistGriddone.cells(nids[i],1).getValue());
			newaddLoginCodes += ((newaddLoginCodes==''?'':',') + userlistGriddone.cells(nids[i],2).getValue());
		}
	}
	if(window.parent.getChooseUsesrResult){
		layoutProgressOn(dhxLayout);
		window.parent.getChooseUsesrResult(ids,names,loginCodes,newaddIds,newaddNames,newaddLoginCodes,deletedIds);
	}
}
	</script>
  </head>
  <body>
  <div id='queryUserDiv' class="formlayout">
	<table>
		<tr>	
			<th>登录账号</th>
			<td>
				<input name="loginCode" id="loginCode" onclick="(arguments[0]||window.event).cancelBubble=true;" onkeyup="queryUser();"/>
			</td>
		</tr>
	</table>
  </div>
  </body>
</html>
